﻿using Newtonsoft.Json;

namespace Infrastructure.Extension
{
    public class EnumNameConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType.IsEnum;
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value is Enum enumValue)
            {
                writer.WriteValue(Enum.GetName(enumValue.GetType(), enumValue));
            }
        }
    }
}
